﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net.Analysis;
using Lucene.Net.Documents;
using Analyzer = Lucene.Net.Analysis.Analyzer;
using StandardAnalyzer = Lucene.Net.Analysis.Standard.StandardAnalyzer;
using Document = Lucene.Net.Documents.Document;
using Field = Lucene.Net.Documents.Field;
using IndexWriter = Lucene.Net.Index.IndexWriter;
using ParseException = Lucene.Net.QueryParsers.ParseException;
using QueryParser = Lucene.Net.QueryParsers.QueryParser;
using Directory = Lucene.Net.Store.Directory;
using RAMDirectory = Lucene.Net.Store.RAMDirectory;
using Version = Lucene.Net.Util.Version;
using IndexSearcher = Lucene.Net.Search.IndexSearcher;
using Query = Lucene.Net.Search.Query;
using ScoreDoc = Lucene.Net.Search.ScoreDoc;
using Store = Lucene.Net.Store;
using Could.Service;
using IO = System.IO;
using System.Diagnostics;
using Lucene.Net.Index;
using MySql.Data.MySqlClient;

namespace CatSentenceAnalyzer
{
    public interface ICATEngine
    {
        void CreateIndex();
        void UpdateIndex();
        void DeleteIndex();
        void SearchIndex();
        void FuzzyIndex();
        void FuzzyIndex(int level);
        void CommandExe();
        void MaketransData();
        void ReadDocs(int min, int max);
        Boolean AddNewIndex();
        string GetTokens(string text, int segtype);
        NPClass m_npc
        {
            get;
            set;
        }
    }
    public interface ICatFactory
    {
        ICATEngine CreateEngine();
    }

    public class PrivateTMSFactory :ICatFactory
    {
        public ICATEngine CreateEngine()
        {
            return new PrivateTMSIndexClass();
        }
    }

    public class MainTMSFactory : ICatFactory
    {
        public ICATEngine CreateEngine()
        {
            return new MainTMSIndexClass();
        }
    }

    public class PrivateTDBFactory : ICatFactory
    {
        public ICATEngine CreateEngine()
        {
            return new PrivateTDBIndexClass();
        }
    }
    public class MainTDBFactory : ICatFactory
    {
        public ICATEngine CreateEngine()
        {
            return new MainTDBIndexClass();
        }
    }

    public abstract class CatIndexClass :ICATEngine
    {
        protected ArrayList m_arrResult;
        protected string m_Indexpath;
        public ISQLEngine m_EngineSql;
        public abstract void CreateIndex();
        public abstract void UpdateIndex();
        public abstract void DeleteIndex();
        public abstract void SearchIndex();
        public abstract void FuzzyIndex();
        public abstract void FuzzyIndex(int level);
        public abstract void MaketransData();
        public abstract Boolean AddNewIndex();
        public abstract Boolean SearchIndex(string key);
        public abstract void ReadDocs(int min, int max);
        public string SegFile = @"segments.gen";
        public Boolean MainIndex;
        public int[] m_Color = new int[5]{0,0x4240094,0x979442,0xc7c465,0x999886 };
        public CatIndexClass()
        {
            m_npc = new NPClass();
            m_arrResult = new ArrayList();
        }
        protected int m_EnginxID{
            get{
                return m_npc.EnginxID;
            }
        }
        private NPClass _m_npc;
        public NPClass m_npc{
            get{
                return _m_npc;
            }
            set{
                _m_npc = value;
            }
        }
        public virtual string GetTokens(string text, int segtype)
        {
            string tmp = "";
            CATSegAnalyzer sa = new CATSegAnalyzer(m_EnginxID, segtype);
            System.IO.StringReader reader = new System.IO.StringReader(text);

            TokenStream ts = sa.ReusableTokenStream("", reader);
            Token tk;
            while ((tk = ts.Next()) != null)
            {
                tmp += tk.TermText() + " ";
            }
            return tmp.TrimEnd();
        }

        public virtual CATSegTokenizer GetTokensList(int itype)
        {
            CATSegAnalyzer sa = new CATSegAnalyzer(m_EnginxID, itype);
            System.IO.StringReader reader = new System.IO.StringReader(m_npc.transdata.OLibrary);

            TokenStream ts = sa.ReusableTokenStream("", reader);
            return (ts as CATSegTokenizer);
        }


        public virtual void CommandExe()
        {
            Stopwatch sWatch = new Stopwatch();
            sWatch.Start();
            if (!IndexDirectory.FileExists(SegFile) && ((ENUMCOMMAND)m_npc.CommandCode!= ENUMCOMMAND.CREATEINDEX))
            {
                CreateIndex();
            }
            switch ((ENUMCOMMAND)(m_npc.CommandCode))
            {
                case ENUMCOMMAND.CREATEINDEX:
                    CreateIndex();
                    break;
                case ENUMCOMMAND.DELECTINDEX:
                    DeleteIndex();
                    break;
                //case ENUMCOMMAND.UPDATEINDEX:
                //    UpdateIndex();
                //    break;
                case ENUMCOMMAND.SAVEINDEX:
                    SearchIndex();
                    break;
                case ENUMCOMMAND.FUZZYINDEX:
                    if (MainIndex)
                    {
                        FuzzyIndex(m_npc.MainFuzzyLevel);
                    }
                    else
                    {
                        FuzzyIndex();
                    }
                    break;
                case ENUMCOMMAND.READDOCUMENTS:
                    ReadDocs(10, 10);
                    break;
            }
            sWatch.Stop();
            m_npc.transdata.ElapsedTime = sWatch.ElapsedMilliseconds;
            sWatch = null;
            MaketransData();

        }

        protected Store.Directory IndexDirectory
        {
            get
            {
                IO.DirectoryInfo info = new IO.DirectoryInfo(m_Indexpath);
                Store.Directory directory = Store.FSDirectory.Open(info);
                return directory;
            }
        }
        protected Analyzer GetCatAnalyzer(int type)
        {
            return new CATSegAnalyzer(m_EnginxID, type );
        }
        protected IndexWriter GetCatWriter(Directory directory, Analyzer analyzer)
        {
            return new IndexWriter( directory , false, analyzer , true);
            //return new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
        }
        protected IndexWriter GetCatAppendWriter(Directory directory, Analyzer analyzer)
        {
            return new IndexWriter(directory, false, analyzer, false);
            //return new IndexWriter(directory, analyzer, false, IndexWriter.MaxFieldLength.LIMITED);
        }
        public enum ENUMCOMMAND
        {
            CREATEINDEX,
            DELECTINDEX,
            //UPDATEINDEX,
            SAVEINDEX,
            FUZZYINDEX,
            READDOCUMENTS
        }

    }

}
